/*
 * Copyright (c) 1998, 2025 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.testing.tests.remote;

import org.eclipse.persistence.descriptors.RelationalDescriptor;
import org.eclipse.persistence.mappings.DirectToFieldMapping;
import org.eclipse.persistence.mappings.OneToManyMapping;
import org.eclipse.persistence.mappings.OneToOneMapping;
import org.eclipse.persistence.sessions.DatabaseLogin;

/**
 * This class was generated by the TopLink project class generator.
 * It stores the meta-data (descriptors) that define the TopLink mappings.
 * @see org.eclipse.persistence.sessions.factories.ProjectClassGenerator
 */
public class MasterSlaveProject extends org.eclipse.persistence.sessions.Project {

    public MasterSlaveProject() {
        setName("MasterSlave");
        applyLogin();

        addDescriptor(buildMasterDescriptor());
        addDescriptor(buildSlaveDescriptor());
    }

    @Override
    public void applyLogin() {
        DatabaseLogin login = new DatabaseLogin();
        setLogin(login);
    }

    public RelationalDescriptor buildMasterDescriptor() {
        RelationalDescriptor descriptor = new RelationalDescriptor();
        descriptor.setJavaClass(org.eclipse.persistence.testing.tests.remote.Master.class);
        descriptor.addTableName("MASTER");
        descriptor.addPrimaryKeyFieldName("MASTER.id");

        // Descriptor properties.
        descriptor.useSoftCacheWeakIdentityMap();
        descriptor.setIdentityMapSize(100);
        descriptor.useRemoteSoftCacheWeakIdentityMap();
        descriptor.setRemoteIdentityMapSize(100);

        // Query manager.
        descriptor.getQueryManager().checkDatabaseForDoesExist();

        // Event manager.

        // Mappings.
        DirectToFieldMapping primaryKeyMapping = new DirectToFieldMapping();
        primaryKeyMapping.setAttributeName("primaryKey");
        primaryKeyMapping.setFieldName("MASTER.id");
        descriptor.addMapping(primaryKeyMapping);

        OneToManyMapping slavesMapping = new OneToManyMapping();
        slavesMapping.setAttributeName("slaves");
        slavesMapping.setReferenceClass(org.eclipse.persistence.testing.tests.remote.Slave.class);
        slavesMapping.useBasicIndirection();
        slavesMapping.addTargetForeignKeyFieldName("SLAVE.mid", "MASTER.id");
        descriptor.addMapping(slavesMapping);

        return descriptor;
    }

    public RelationalDescriptor buildSlaveDescriptor() {
        RelationalDescriptor descriptor = new RelationalDescriptor();
        descriptor.setJavaClass(org.eclipse.persistence.testing.tests.remote.Slave.class);
        descriptor.addTableName("SLAVE");
        descriptor.addPrimaryKeyFieldName("SLAVE.id");

        // Descriptor properties.
        descriptor.useSoftCacheWeakIdentityMap();
        descriptor.setIdentityMapSize(100);
        descriptor.useRemoteSoftCacheWeakIdentityMap();
        descriptor.setRemoteIdentityMapSize(100);

        // Query manager.
        descriptor.getQueryManager().checkDatabaseForDoesExist();

        // Event manager.

        // Mappings.
        DirectToFieldMapping MyPrimaryKeyMapping = new DirectToFieldMapping();
        MyPrimaryKeyMapping.setAttributeName("primaryKey");
        MyPrimaryKeyMapping.setFieldName("SLAVE.id");
        descriptor.addMapping(MyPrimaryKeyMapping);

        OneToOneMapping masterMapping = new OneToOneMapping();
        masterMapping.setAttributeName("master");
        masterMapping.setReferenceClass(org.eclipse.persistence.testing.tests.remote.Master.class);
        masterMapping.dontUseIndirection();
        masterMapping.addForeignKeyFieldName("SLAVE.mid", "MASTER.id");
        descriptor.addMapping(masterMapping);

        return descriptor;
    }

}

